#include <stdio.h>
#include <stdlib.h>

typedef int ElementType;
typedef struct TNode *Position;
typedef Position BinTree;
struct TNode{
    ElementType Data;
    BinTree Left;
    BinTree Right;
};

/* 先序遍历，由裁判实现，细节不表 */
void PreorderTraversal( BinTree BT ) {
    if (BT == NULL) {
        return;
    }
    printf(" %d", BT->Data);
    PreorderTraversal(BT->Left);
    PreorderTraversal(BT->Right);
} 

/* 中序遍历，由裁判实现，细节不表 */
void InorderTraversal( BinTree BT ) {
    if (BT == NULL) {
        return;
    }
    InorderTraversal(BT->Left);
    printf(" %d", BT->Data);
    InorderTraversal(BT->Right);
}

BinTree Insert( BinTree BST, ElementType X );
BinTree Delete( BinTree BST, ElementType X );
Position Find( BinTree BST, ElementType X );
Position FindMin( BinTree BST );
Position FindMax( BinTree BST );

int main()
{
    BinTree BST, MinP, MaxP, Tmp;
    ElementType X;
    int N, i;

    BST = NULL;
    scanf("%d", &N);
    for ( i=0; i<N; i++ ) {
        scanf("%d", &X);
        BST = Insert(BST, X);
    }

    printf("Preorder:"); PreorderTraversal(BST); printf("\n");
    MinP = FindMin(BST);
    MaxP = FindMax(BST);

    scanf("%d", &N);
    for( i=0; i<N; i++ ) {
        scanf("%d", &X);
        Tmp = Find(BST, X);
        if (Tmp == NULL) printf("%d is not found\n", X);
        else {
            printf("%d is found\n", Tmp->Data);
            if (Tmp==MinP) printf("%d is the smallest key\n", Tmp->Data);
            if (Tmp==MaxP) printf("%d is the largest key\n", Tmp->Data);
        }
    }
    scanf("%d", &N);
    for( i=0; i<N; i++ ) {
        scanf("%d", &X);
        BST = Delete(BST, X);
    }
    printf("Inorder:"); InorderTraversal(BST); printf("\n");

    return 0;
}

BinTree Insert( BinTree BST, ElementType X ) {
    if (BST == NULL) {
        BST = (Position) malloc(sizeof(struct TNode));
        BST->Data = X;
        BST->Left = BST->Right = NULL;
        return BST;
    }
    if (X < BST->Data) {
        BST->Left = Insert(BST->Left, X);
    } else  {
        BST->Right = Insert(BST->Right, X);
    }
    return BST;
}

BinTree Delete( BinTree BST, ElementType X ) {
    // 如果X不在树中，则打印一行Not Found并返回原树的根结点指针；
    if (BST == NULL) {
        printf("Not Found\n");
    } else if (X < BST->Data) {
        BST->Left = Delete(BST->Left, X);
    } else  if (X > BST->Data) {
        BST->Right = Delete(BST->Right, X);
    } else if (BST->Left != NULL && BST->Right != NULL) {
        // 找到要删除的节点
        // 在右子树中找最小
        Position t = FindMin(BST->Right);
        BST->Data = t->Data;
        BST->Right = Delete(BST->Right, BST->Data);
    } else {
        Position t = BST;
        if (BST->Right != NULL) {
            BST = BST->Right;
        } else if (BST->Left != NULL) {
            BST = BST->Left;
        }
        // free(t);
    }
    return BST;
}

Position Find( BinTree BST, ElementType X ) {
    if (BST == NULL) {
        return NULL;
    }
    for (Position p = BST; p!=NULL; ) {
        if (p->Data == X) {
            return p;
        }
        if (X < p->Data) {
            p = p->Left;
        } else {
            p = p->Right;
        }
    }
    return NULL;
}

Position FindMin( BinTree BST ) {
    if (BST == NULL) {
        return NULL;
    }
    Position p = BST;
    while (p->Left != NULL) {
        p = p->Left;
    }
    return p;
}

Position FindMax( BinTree BST ) {
    if (BST == NULL) {
        return NULL;
    }
    Position p = BST;
    while (p->Right != NULL) {
        p = p->Right;
    }
    return p;
}
